package dao.impl;

import dao.AdminsDao;
import entity.Admins;
import entity.vo.DoctorsGetAppointments;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import utils.DruidUtils;

import java.sql.Connection;
import java.util.List;

public class AdminsDaoImpl implements AdminsDao {

    @Override
    public Admins queryById(Integer id) {
        return null;
    }

    @Override
    public List<DoctorsGetAppointments> queryAllAppointments() {
        Connection conn = null;
        try {
            //从连接池获取连接
            conn = DruidUtils.getConnection();
            String s = "SELECT  a.id ,datetime,status,name,email,phone_number\n" +
                    "                                FROM appointments a join users u on a.user_id = u.id order by datetime desc";
            BasicRowProcessor basicRowProcessor = new BasicRowProcessor(new GenerousBeanProcessor());
            QueryRunner queryRunner = new QueryRunner();
            List<DoctorsGetAppointments> query = queryRunner.query(conn, s,
                    new BeanListHandler<>(DoctorsGetAppointments.class, basicRowProcessor));
            DruidUtils.close(null, conn);
            return query;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //归还连接(没有Statement)
            DruidUtils.close(null, conn);
        }
        return null;
    }
}
